<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of uploadFile
 *
 * @author HIEN JENNY
 */
class Libs_uploadFile {

    public function getExtension($str) {
        $i = strrpos($str, ".");
        if (!$i) {
            return "";
        }
        $l = strlen($str) - $i;
        $ext = substr($str, $i + 1, $l);
        return $ext;
    }

    public function checkUploadImage($array) {
        define("MAX_SIZE", "100");
        $flag = false;
        $error = $_FILES['image']['error'];
        $array = $_FILES['image'];
        $image_name = $_FILES['image']['name'];
        $tmp_name = $_FILES['image']['tmp_name'];
        if ($image_name) {
            $filename = stripslashes($_FILES['image']['name']);
            $extension = $this->getExtension($filename);
            $extension = strtolower($extension);
            if (($extension != "jpg") && ($extension != "png") && ($extension != "jpeg") && ($extension != gif)) {
                echo "<script>alert('Đây không phải là file ảnh, mời bạn chọn file khác')</script>";
                $flag = false;
            } else {
                $size = filesize($_FILES['image']['tmp_name']);
                if ($size > MAX_SIZE * 1024) {
                    echo "<script>alert('Vượt quá dung lượng cho phép')</script>";
                    $flag = false;
                } else {
                    if ($error == 0) {
                        if (!file_exists("templates/admin/images/product/" . $image_name)) {
                            move_uploaded_file($tmp_name, "templates/admin/images/product/" . $image_name);
                            $flag = true;
                        } else {
                            echo "<script>alert('Đã tồn tại file,mời bạn chọn file khác')</script>";
                            $flag = false;
                        }
                    }
                }
            }
        }
        return $flag;
    }

}

?>
